package com.financing.wap.web.controller.api;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstUserSettingDTO;
import com.financing.wap.service.IDstUserSettingService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;
/***
 * 用户设置
 *
 */
@Controller("dstUserSettingController")
@RequestMapping("/api/dstUserSetting")
public class DstUserSettingController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserSettingService dstUserSettingService;
	private static final  Object obj = new Object();
	
	/***
	 * 用户设置详情
	 * @param request
	 * @param dstUserSetting
	 * @return
	 */
	@RequestMapping("/detailDstUserSetting")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail dstUserSetting  begin .......");
			String openid=request.getParameter("openid");
			//参数  默认
			if(StringUtils.isBlank(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			DstUserSettingDTO dstUserSetting =new DstUserSettingDTO();
			dstUserSetting.setUserOpenid(openid);
			dstUserSetting.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			DstUserSettingDTO dto = dstUserSettingService.queryListGetFirst(dstUserSetting);
			if(StringUtils.isBlank(dto.getMessageDataSend())){
				//默认打开
				dto.setMessageDataSend("1");
			}
			if(StringUtils.isBlank(dto.getMessageSendNight())){
				dto.setMessageSendNight("1");//夜间勿扰
			}
			if(StringUtils.isBlank(dto.getMessageArticleSend())){
				dto.setMessageArticleSend("1");//默认上午热文推送
			}
			
			if(StringUtils.isBlank(dto.getMessageSendSetting())){
				//默认立即提醒
				dto.setMessageSendSetting("1");
			}
			
			result.setAttribute("dto",dto);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("query detail dstUserSetting  end .......");
		}catch(Exception e){
			log.error("query detail dstUserSetting error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 修改用户设置
	 * @param request
	 * @param dstUserSetting
	 * @return
	 */
	@RequestMapping("/updateDstUserSetting")
	@ResponseBody
	public ResultMap update(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update dstUserSetting  begin .......");
			String openid=request.getParameter("openid");
			
			String id=request.getParameter("id");
			String messageSendSetting=request.getParameter("messageSendSetting");
			String messageDataSend=request.getParameter("messageDataSend");
			String messageArticleSend=request.getParameter("messageArticleSend");
			String messageSendNight=request.getParameter("messageSendNight");
			//参数  默认
			if(StringUtils.isBlank(openid)||StringUtils.isBlank(messageSendSetting)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			if(StringUtils.isBlank(messageDataSend)){
				messageDataSend="1";//默认打开
			}
			if(StringUtils.isBlank(messageSendNight)){
				messageSendNight="2";//否
			}
			DstUserSettingDTO dstUserSetting=new DstUserSettingDTO();
			dstUserSetting.setMessageSendSetting(messageSendSetting);
			dstUserSetting.setMessageDataSend(messageDataSend);
			dstUserSetting.setMessageArticleSend(messageArticleSend);
			dstUserSetting.setMessageSendNight(messageSendNight);
			//判断
			if(StringUtil.isNullOrEmptyStr(id)){
				synchronized (obj) {
					//查询当前用户
					DstUserSettingDTO query =new DstUserSettingDTO();
					query.setUserOpenid(openid);
					query.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
					DstUserSettingDTO dto = dstUserSettingService.queryListGetFirst(query);
					if(dto.getId()==null){
						dstUserSetting.setUserOpenid(openid);
						dstUserSettingService.insert(dstUserSetting);
					}else{
						dto.setMessageSendSetting(messageSendSetting);
						dto.setMessageDataSend(messageDataSend);
						dto.setMessageArticleSend(messageArticleSend);
						dto.setMessageSendNight(messageSendNight);
						dstUserSettingService.update(dto);
					}
				}
			}else{
				dstUserSetting.setId(Integer.parseInt(id));
				dstUserSettingService.update(dstUserSetting);
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update dstUserSetting list end .......");
		}catch(Exception e){
			log.error("update dstUserSetting error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
